﻿using Architecture.Domain.Services.Interface;
using Architecture.Domain.TraceabilityManage;
using Architecture.Domian.DTO;
using Architecture.Repository.Impl;
using Architecture.Repository.Interface;
using System.Security.Cryptography;

namespace Architecture.Domain.Services.Impl
{
    public class DeepProcessingService : IDeepProcessingService
    {

        private readonly IDeepProcessingRepository _deepProcessingRepository;
        private readonly ICorporationRepository _corporationRepository;
        private readonly ISupplierRepository _supplierRepository;


        public DeepProcessingService(
            IDeepProcessingRepository deepProcessingRepository,
            ICorporationRepository corporationRepository,
            ISupplierRepository supplierRepository)
        {
            _deepProcessingRepository = deepProcessingRepository;
            _corporationRepository = corporationRepository;
            _supplierRepository = supplierRepository;
        }

        public async Task<int> AddDeepProcessing(DeepProcessing deep)
        {
            return await _deepProcessingRepository.AddAsync(deep);
        }

        public async Task<int> AddCorporation(Corporation corporation)
        {
            var deepProcessing = await _deepProcessingRepository.GetByFuncAsync(x => x.ProductionLot == corporation.ProductionLot && !x.IsDelete);

            if (deepProcessing == null)
            {
                await _corporationRepository.AddAsync(corporation);

                DeepProcessing deepProcessing1 = new()
                {
                    DeepProcessingCompany = corporation.CId,
                    ProductionLot = corporation.ProductionLot
                };
                return await AddDeepProcessing(deepProcessing1);
            }

            await _corporationRepository.AddAsync(corporation);

            deepProcessing.DeepProcessingCompany = corporation.CId;

            return await _deepProcessingRepository.UpdateAsync(deepProcessing);
        }

        public async Task<int> BatchCorporationPicList(string? productionLot, string? urlImg, int dpId)
        {
            var corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (corporation == null)
            {
                await AddCorporation(new Corporation { ProductionLot = productionLot });

                corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

                if (dpId == 0)
                {
                    await AddDeepProcessing(new DeepProcessing { ProductionLot = productionLot, DeepProcessingCompany = corporation.CId });
                }

                corporation.CompanyPicture = urlImg;
                return await _corporationRepository.UpdateAsync(corporation);
            }
            else
            {
                corporation.CompanyPicture = urlImg;
                return await _corporationRepository.UpdateAsync(corporation);
            }
        }

        public async Task<int> BatchDeepProcessingPicList(string? productionLot, string? urlImg)
        {
            var deepProcessing = await _deepProcessingRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (deepProcessing == null)
            {
                DeepProcessing deepProcessing1 = new()
                {
                    ProductionLot = productionLot,
                    FoodSafetyInformationPicture = urlImg,
                };
                return await AddDeepProcessing(deepProcessing1);
            }
            else
            {
                deepProcessing.FoodSafetyInformationPicture = urlImg;

                return await _deepProcessingRepository.UpdateAsync(deepProcessing);
            }
        }

        public async Task<Corporation> GetCorporationInfo(string? productionLot, int DeepProcessingCompanyId)
        {
            var result = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete && x.CId == DeepProcessingCompanyId);
            return result;
        }

        public async Task<List<PictureDTO>> GetCorporationPicture(string? productionLot, int DeepProcessingCompanyId)
        {
            var corporation = await _corporationRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete && x.CId == DeepProcessingCompanyId);

            if (corporation == null || string.IsNullOrEmpty(corporation.CompanyPicture))
            {
                return new List<PictureDTO>();
            }

            var result = corporation.CompanyPicture.Split(',').Select(x => new PictureDTO
            {
                name = x,
                url = "http://localhost:5124/Images/" + x
            }).ToList();

            return result;
        }

        public async Task<DeepProcessing> GetDeepProcessingInfo(string? productionLot)
        {
            var result = await _deepProcessingRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);
            return result;
        }

        public async Task<List<PictureDTO>> GetDeepProcessingPicture(string? productionLot)
        {
            var deepProcessing = await _deepProcessingRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete);

            if (deepProcessing == null || string.IsNullOrEmpty(deepProcessing.FoodSafetyInformationPicture))
            {
                return new List<PictureDTO>();
            }

            return deepProcessing.FoodSafetyInformationPicture.Split(',').Select(x => new PictureDTO
            {
                name = x,
                url = "http://localhost:5124/Images/" + x
            }).ToList();
        }

        public async Task<int> UpdateCorporation(Corporation corporation)
        {
            var deepProcessing = await _deepProcessingRepository.GetByFuncAsync(x => x.ProductionLot == corporation.ProductionLot && !x.IsDelete);

            if (deepProcessing == null)
            {
                DeepProcessing deepProcessing1 = new()
                {
                    DeepProcessingCompany = corporation.CId,
                    ProductionLot = corporation.ProductionLot
                };

                return await AddDeepProcessing(deepProcessing1);
            }

            deepProcessing.DeepProcessingCompany = corporation.CId;

            await UpdateDeepProcessing(deepProcessing);

            return await _corporationRepository.UpdateAsync(corporation);
        }

        public async Task<int> UpdateDeepProcessing(DeepProcessing deep)
        {
            return await _deepProcessingRepository.UpdateAsync(deep);
        }

        public async Task<int> BatchSupplier(Supplier supplier, int flag)
        {
            var supplier1 = await _supplierRepository.GetByFuncAsync(x => x.ProductionLot == supplier.ProductionLot && !x.IsDelete);

            if (supplier1 == null)
            {
                Supplier addSupplier = new();

                addSupplier.ProductionLot = supplier.ProductionLot;

                switch (flag)
                {
                    //原料
                    case 1:
                        addSupplier.RawMaterialPicture = supplier.RawMaterialPicture;
                        break;
                    //辅料
                    case 2:
                        addSupplier.AccessoriesPicture = supplier.AccessoriesPicture;
                        break;
                    //添加剂
                    case 3:
                        addSupplier.AdditivePicture = supplier.AdditivePicture;
                        break;
                    //包材
                    case 4:
                        addSupplier.PackagingMaterialPicture = supplier.PackagingMaterialPicture;
                        break;
                }

                return await _supplierRepository.AddAsync(addSupplier);
            }

            switch (flag)
            {
                //原料
                case 1:
                    supplier1.RawMaterialPicture = supplier.RawMaterialPicture;
                    break;
                //辅料
                case 2:
                    supplier1.AccessoriesPicture = supplier.AccessoriesPicture;
                    break;
                //添加剂
                case 3:
                    supplier1.AdditivePicture = supplier.AdditivePicture;
                    break;
                //包材
                case 4:
                    supplier1.PackagingMaterialPicture = supplier.PackagingMaterialPicture;
                    break;
            }

            return await _supplierRepository.UpdateAsync(supplier1);
        }

        public async Task<List<PictureListDTO>> GetSupplierPicture(string? productionLot)
        {

            var supplier = await _supplierRepository.GetByFuncAsync(x => x.ProductionLot == productionLot && !x.IsDelete); if (supplier == null) return null;

            var result = new List<PictureListDTO>();

            if (!string.IsNullOrEmpty(supplier.RawMaterialPicture))
            {
                result.Add(new PictureListDTO
                {
                    flag = 1,
                    dtos = supplier.RawMaterialPicture.Split(',').Select(x => new PictureDTO
                    {
                        name = x,
                        url = "http://localhost:5124/Images/" + x
                    }).ToList()
                });
            }

            if (!string.IsNullOrEmpty(supplier.AccessoriesPicture))
            {
                result.Add(new PictureListDTO
                {
                    flag = 2,
                    dtos = supplier.AccessoriesPicture.Split(',').Select(x => new PictureDTO
                    {
                        name = x,
                        url = "http://localhost:5124/Images/" + x
                    }).ToList()
                });
            }

            if (!string.IsNullOrEmpty(supplier.AdditivePicture))
            {
                result.Add(new PictureListDTO
                {
                    flag = 3,
                    dtos = supplier.AdditivePicture.Split(',').Select(x => new PictureDTO
                    {
                        name = x,
                        url = "http://localhost:5124/Images/" + x
                    }).ToList()
                });
            }

            if (!string.IsNullOrEmpty(supplier.PackagingMaterialPicture))
            {
                result.Add(new PictureListDTO
                {
                    flag = 4,
                    dtos = supplier.PackagingMaterialPicture.Split(',').Select(x => new PictureDTO
                    {
                        name = x,
                        url = "http://localhost:5124/Images/" + x
                    }).ToList()
                });
            }

            return result;
        }
    }
}
